define(function(require, exports, module) {
    var github = {
        domain: 'https://api.github.com',
        debug: true
    };
    var rest = require('rest');
    var accessToken = 'access_token=3414bf79549a120d59c9eb3a3ab71893e22c6d33';

    //doc: http://developer.github.com/v3/issues/#list-issues-for-a-repository
    github.ISSUES = github.domain + '/repos/vincenthou/vincenthou.github.io/issues';
    github.url = {
        ISSUE_DETAIL: github.ISSUES,
        BLOG_LIST: github.ISSUES + '?labels=blog&' + accessToken,
        COLLECTION_LIST: github.ISSUES + '?labels=collection&' + accessToken,
        PROJECT_LIST: github.ISSUES + '/13?' + accessToken,
        REPO_LIST: github.domain + '/users/vincenthou/repos?' + accessToken,
        DEBUG_JSON: github.ISSUES + '/19?' + accessToken
    };

    //add loading image when sending request
    rest.before = function() {
        $('#loading-img').fadeIn(1000);
    };

    rest.after = function() {
        $('#loading-img').fadeOut(1000);
    };

    github.listBlogs = function(cb) {
        rest.get(github.url.BLOG_LIST, function(data) {
            cb(data);
        });
    };

    github.listProjects = function(cb) {
        rest.get(github.url.PROJECT_LIST, function(data) {
            var rawBody = data.body;
            rawBody = rawBody.replace(/`{3}\w*/gi,'');
            cb(JSON.parse(rawBody));
        });
    };

    github.listRepos = function(cb) {
        rest.get(github.url.REPO_LIST, function(repos) {
            cb(repos);
        });
    };

    github.debugJSON = function(cb) {
        if (github.debug) {
            require.async('../../test/pens.json', function(data){
                cb(data);
            });
        } else {
            rest.get(github.url.DEBUG_JSON, function(data) {
                var rawBody = data.body;
                rawBody = rawBody.replace(/`{3}\w*/gi,'');
                cb(JSON.parse(rawBody));
            });
        }
    };

    return github;
});
